<?php

class AdminController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
            $posts=new Model_Posts();
                        $res = $posts->getPostswithCommnets();
                        $this->view->results = $res;
    }

    public function addAction()
    {
        $postForm = new Form_Addpost();
             if (Zend_Auth::getInstance()->hasIdentity()) {
                $whois=Zend_Auth::getInstance()->getIdentity();
                $acces_control=new Model_Acl();
                if ($acces_control->isAllowed($whois->Role, 'posts', 'add')) {
                    //$postForm = new Form_Addpost();
                     if ($this->getRequest()->isPost()) {
                         if ($postForm->isValid($this->getRequest()->getPost())) {

                                $save_form = new Model_Posts();
                                $save_form->savePost($postForm->getValues());
                                $this->_redirect('admin/index');

                         } else {
                            $message="Please do things properly and do not try to break the system.";
                         }
                     } else {
                         $message="";
                     }
                } else {
                    $message="You are not allowed to add a Post.";
                }
            } else {
                $message="Access Denied.";
            }

            //$addForm = new Form_Addpost();
            $this->view->displayError = $message;
            $this->view->displayForm = $postForm;
    }

    public function editAction()
    {
        $postForm = new Form_Addpost();
             if (Zend_Auth::getInstance()->hasIdentity()) {
                $whois=Zend_Auth::getInstance()->getIdentity();
                $acces_control=new Model_Acl();
                $postid = (int)$this->_getParam('id');
                if ($acces_control->isAllowed($whois->Role, 'posts', 'edit')) {

                    if ($this->getRequest()->isPost()) {
                         if ($postForm->isValid($this->getRequest()->getPost())) {
                                $idPost = $this->getRequest()->getPost('post_id');
                                $save_form = new Model_Posts();
                                $save_form->updatePost($postForm->getValues($idPost));
                                $this->_redirect('admin/index');

                         } else {
                            $message="Please do things properly and do not try to break the system.";
                         }
                     } else {
                            $populate_form = new Model_Posts();
                            $result = $populate_form->getPost($postid);
                            $postForm->populate($result);
                            $message="";
                     }
                } else {
                    $message="You are not allowed to edit a Post.";
                }
            } else {
                $message="Access Denied.";
            }

            //$addForm = new Form_Addpost();
            $this->view->displayError = $message;
            $this->view->displayForm = $postForm;
    }

    public function deleteAction()
    {
        if (Zend_Auth::getInstance()->hasIdentity()) {
            $whoAmI = Zend_Auth::getInstance()->getIdentity();
            $control_access =new Model_Acl;
            if ($control_access->isAllowed('admin', 'posts', 'delete')) {
                $postid = (int)$this->_getParam('id');
                $delete_form=new Model_Posts();
                $isdone=$delete_form->deletePost($postid);
                if ($isdone) {
                    $this->view->displayMessage = "Post Deleted Successfully.";
                } else {
                    $this->view->displayMessage = "Unable to delete Post.";
                }
            }
        }
    }
}







